package thread;

/**
 * Created with IntelliJ IDEA.
 * Description: 线程中断代码测试
 * User: 王圆豪
 * Date: 2025-04-23
 * Time: 20:28
 */
public class Demo11 {
    public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread(()->{
            while (!Thread.currentThread().isInterrupted()){
                System.out.println("Hello thread!");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
//                    throw new RuntimeException(e);
                }
            }
            System.out.println("thread线程被中断！");
        });

        thread.start();
        Thread.sleep(3000);
        System.out.println("main线程尝试中断thread线程！");
        thread.interrupt();
    }
}
